package com.hgyc.mom.tool.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.hgyc.mom.tool.entity.AutoCodeRule;
import com.hgyc.mom.tool.service.AutoCodeRuleService;
import com.hgyc.mom.tool.vo.AutoCodeRuleParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 编码生成规则Controller
 *
 * @author fwj
 * @since 2025-05-06
 */
@Tag(name = "编码生成规则控制器", description = "编码生成规则控制器")
@RestController
@RequestMapping("/tool/rule")
public class AutoCodeRuleController {
    @Autowired
    private AutoCodeRuleService autoCodeRuleService;

    @Operation(summary = "查询编码生成规则列表", description = "查询编码生成规则列表")
    @PostMapping("/pageList")
    public Result<IPage<AutoCodeRule>> pageList(@Parameter(description = "查询参数") @RequestBody AutoCodeRuleParam autoCodeRuleParam) {
        Page<AutoCodeRule> page = new Page<>(autoCodeRuleParam.getPage(), autoCodeRuleParam.getPageSize());
        AutoCodeRule autoCodeRule = new AutoCodeRule();
        BeanUtils.copyProperties(autoCodeRuleParam, autoCodeRule);
        QueryWrapper<AutoCodeRule> wrapper = QueryWrapperUtil.build(autoCodeRule);
        IPage<AutoCodeRule> list = autoCodeRuleService.page(page, wrapper);
        return Result.success(list);
    }

    @Operation(summary = "获取编码规则下拉列表", description = "获取编码规则下拉列表")
    @GetMapping("/selectList")
    public Result<List<AutoCodeRule>> selectList() {
        return Result.success(autoCodeRuleService.list());
    }

    @Operation(summary = "获取编码生成规则详细信息", description = "获取编码生成规则详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "编码生成规则ID") @PathVariable("id") Long id) {
        return Result.success(autoCodeRuleService.getById(id));
    }

    @Operation(summary = "新增编码生成规则", description = "新增编码生成规则")
    @PostMapping
    public Result save(@Parameter(description = "编码生成规则") @RequestBody AutoCodeRule autoCodeRule) {
        return Result.success(autoCodeRuleService.save(autoCodeRule));
    }

    @Operation(summary = "修改编码生成规则", description = "修改编码生成规则")
    @PutMapping
    public Result update(@Parameter(description = "编码生成规则") @RequestBody AutoCodeRule autoCodeRule) {
        return Result.success(autoCodeRuleService.updateById(autoCodeRule));
    }

    @Operation(summary = "删除编码生成规则", description = "删除编码生成规则")
    @DeleteMapping
    public Result remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        autoCodeRuleService.removeCodeRule(Arrays.asList(ids));
        return Result.success();
    }
}
